namespace OpenComposite.Inbox.Forms.SelfService
{
	using System;
	using System.Collections;
	using System.ComponentModel;
	using System.Drawing;
	using System.Windows.Forms;

	using Microsoft.Win32;
	using OpenComposite.Inbox.Data;
	using OpenComposite.Forms;

	public class FormChangePassword : OpenComposite.Inbox.Forms.SelfService.FormBase
	{
		#region Initialization

		public FormChangePassword()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		#endregion Initialization

		#region Protected Members

		#region Protected Methods

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if ( disposing ) {
				if ( components != null ) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		protected override bool SaveAndExit()
		{
			try {
				if ( textBoxOld.Text == "" || textBoxNew1.Text == "" || textBoxNew2.Text == "" ) {
					MessageBox.Show("Please fill all entry fields!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
					return false;
				}

				if ( textBoxNew1.Text != textBoxNew2.Text ) {
					MessageBox.Show("The new and the repeated password are not the same.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
					textBoxNew1.Text = "";
					textBoxNew1.Focus();
					textBoxNew2.Text = "";
					return false;
				}

				if ( !Global.DataAccess.UpdateUserPassword(textBoxOld.Text, textBoxNew1.Text) ) {
					MessageBox.Show("Error while updating the password. Please try again.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
					textBoxOld.Text = "";
					textBoxOld.Focus();
					textBoxNew1.Text = "";
					textBoxNew2.Text = "";
					return false;
				} else {
					Global.DataAccess.SetOption("pwd", "");
					MessageBox.Show("Password was changed successfully.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
					return true;
				}
			} catch ( Exception ex ) {
				ErrorBox.Show(ex, this.Text);
				return false;
			}
		}

		#endregion Protected Methods

		#endregion Protected Members

		#region Private Members

		#region Private Fields

		private System.ComponentModel.IContainer components = null;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textBoxNew1;
		private System.Windows.Forms.TextBox textBoxNew2;
		private System.Windows.Forms.TextBox textBoxOld;

		#endregion Private Fields

		#region Private Methods

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormChangePassword));
			this.textBoxOld = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.textBoxNew1 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.textBoxNew2 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label14 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// textBoxOld
			// 
			this.textBoxOld.Location = new System.Drawing.Point(144, 39);
			this.textBoxOld.Name = "textBoxOld";
			this.textBoxOld.PasswordChar = '*';
			this.textBoxOld.Size = new System.Drawing.Size(177, 20);
			this.textBoxOld.TabIndex = 0;
			this.textBoxOld.Tag = "first_nm";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ));
			this.label1.Location = new System.Drawing.Point(59, 42);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(79, 14);
			this.label1.TabIndex = 45;
			this.label1.Text = "Old Password:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// textBoxNew1
			// 
			this.textBoxNew1.Location = new System.Drawing.Point(144, 63);
			this.textBoxNew1.Name = "textBoxNew1";
			this.textBoxNew1.PasswordChar = '*';
			this.textBoxNew1.Size = new System.Drawing.Size(177, 20);
			this.textBoxNew1.TabIndex = 1;
			this.textBoxNew1.Tag = "first_nm";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ));
			this.label2.Location = new System.Drawing.Point(52, 66);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(86, 14);
			this.label2.TabIndex = 47;
			this.label2.Text = "New Password:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// textBoxNew2
			// 
			this.textBoxNew2.Location = new System.Drawing.Point(144, 87);
			this.textBoxNew2.Name = "textBoxNew2";
			this.textBoxNew2.PasswordChar = '*';
			this.textBoxNew2.Size = new System.Drawing.Size(177, 20);
			this.textBoxNew2.TabIndex = 2;
			this.textBoxNew2.Tag = "first_nm";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ));
			this.label3.Location = new System.Drawing.Point(15, 90);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(123, 14);
			this.label3.TabIndex = 49;
			this.label3.Text = "Repeat New Password:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label14
			// 
			this.label14.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ));
			this.label14.ForeColor = System.Drawing.Color.Red;
			this.label14.Location = new System.Drawing.Point(324, 39);
			this.label14.Name = "label14";
			this.label14.Size = new System.Drawing.Size(9, 20);
			this.label14.TabIndex = 84;
			this.label14.Text = "*";
			this.label14.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ));
			this.label4.ForeColor = System.Drawing.Color.Red;
			this.label4.Location = new System.Drawing.Point(324, 63);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(9, 20);
			this.label4.TabIndex = 85;
			this.label4.Text = "*";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ));
			this.label5.ForeColor = System.Drawing.Color.Red;
			this.label5.Location = new System.Drawing.Point(324, 87);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(9, 20);
			this.label5.TabIndex = 86;
			this.label5.Text = "*";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// FormChangePassword
			// 
			this.ClientSize = new System.Drawing.Size(342, 124);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label14);
			this.Controls.Add(this.textBoxNew2);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.textBoxNew1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.textBoxOld);
			this.Controls.Add(this.label1);
			this.Icon = ( (System.Drawing.Icon)( resources.GetObject("$this.Icon") ) );
			this.Name = "FormChangePassword";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Change Password";
			this.Controls.SetChildIndex(this.label1, 0);
			this.Controls.SetChildIndex(this.textBoxOld, 0);
			this.Controls.SetChildIndex(this.label2, 0);
			this.Controls.SetChildIndex(this.textBoxNew1, 0);
			this.Controls.SetChildIndex(this.label3, 0);
			this.Controls.SetChildIndex(this.textBoxNew2, 0);
			this.Controls.SetChildIndex(this.label14, 0);
			this.Controls.SetChildIndex(this.label4, 0);
			this.Controls.SetChildIndex(this.label5, 0);
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion Private Methods

		#endregion Private Members
	}
}